<?php
namespace app\run\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Validate;
class Index extends Run
{
	public $tabledata;
	public function _initialize()
    {
        call_user_func(array('parent', __FUNCTION__));
    }

	public function index()
	{


		return view();
	}
	public function  driver()
	{
		
		$res = Db::name('user')->select();
		$this->assign('res',$res);
		return view();
	}
 
	public function welcome()
	
	
	{


		return view();
	}
	public function importExcel()  
	{
		//import('phpexcel.PHPExcel', EXTEND_PATH);//方法二  
		vendor("PHPExcel.PHPExcel"); //方法一  
		$objPHPExcel = new \PHPExcel();  
		if (request()->isPost()) {  
			$file = request()->file('file'); 
			
			$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'excel');  
			if($info){
			 
				$exclePath = $info->getSaveName();//获取文件名  
				$file_name = ROOT_PATH . 'public' . DS . 'uploads'. DS . 'excel' . DS . $exclePath;   //上传文件的地址  
				
				$objReader =\PHPExcel_IOFactory::createReader('Excel2007');  
				$obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8  
				// echo "<pre>	"; 
				$excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式  
				array_shift($excel_array);  //删除第一个数组(标题);  
				$data = [];  
				$i=0;  
				foreach($excel_array as $k=>$v) {  
					$data[$k]['license_plate_number'] = $v[0];  
					$data[$k]['vin'] = $v[1];  
					$data[$k]['engine_no'] = $v[2];  
					
					$i++;  
				}  
				// return json(array('file'=>$data)); 
				
			   $success=Db::name('driver')->insertAll($data); //批量插入数据  
			   $this->tabledata = $data;
			   //$i=  
			   $error=$i-$success;  
				return json(array('status'=>1,'msg'=> "总{$i}条，成功{$success}条，失败{$error}条。",'result'=>$data)); 
			   // Db::name('t_station')->insertAll($city); //批量插入数据  
			}
			else{
				return json(array('file'=> $file->getError()));
			
			}
		}
	}
	public function tableData(){
		$res = Db::name('driver')->select();
		return json(array('code'=>0,'msg'=>'success','count'=>count($res),'data'=>$res));
	}
	
	 
}